{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "226eb046-2ce9-4448-9a22-bcdc3d83538e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入一个奇数N的值:  1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n"
     ]
    }
   ],
   "source": [
    "def print_diamond(N):\n",
    "    # 验证N是否为奇数\n",
    "    if N % 2 == 0:\n",
    "        print(\"请输入一个奇数作为N的值。\")\n",
    "        return\n",
    "    \n",
    "    # 打印菱形的上半部分，包含中间行\n",
    "    for i in range(1, (N + 1) // 2 + 1):\n",
    "        # 计算空格数和星号数\n",
    "        spaces = ' ' * ((N - 2 * i) // 2)\n",
    "        stars = '*' * (2 * i - 1)\n",
    "        # 打印当前行\n",
    "        print(spaces + stars)\n",
    "    \n",
    "    # 打印菱形的下半部分，不包含中间行\n",
    "    for i in range((N - 1) // 2, 0, -1):\n",
    "        # 计算空格数和星号数（与上半部分相同）\n",
    "        spaces = ' ' * ((N - 2 * i) // 2)\n",
    "        stars = '*' * (2 * i - 1)\n",
    "        # 打印当前行\n",
    "        print(spaces + stars)\n",
    "\n",
    "# 主程序执行部分\n",
    "try:\n",
    "    # 从用户处获取输入，并尝试转换为整数\n",
    "    N = int(input(\"请输入一个奇数N的值: \"))\n",
    "    # 调用函数打印菱形\n",
    "    print_diamond(N)\n",
    "except ValueError:\n",
    "    # 如果输入的不是整数，则打印错误信息\n",
    "    print(\"输入无效，请输入一个整数。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d948700-d191-4dce-aef6-ae6991232c41",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
